package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.activity.recipe.PERecipeActivity;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.DefectCodeEntity;
import com.glory.brepda.entity.EqpEntity;
import com.glory.brepda.entity.EqpUnlockEntity;
import com.glory.brepda.entity.ScrapEntity;
import com.glory.brepda.entity.ToolEntity;
import com.glory.brepda.entity.UpdateIPEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentManager;
import butterknife.BindView;
import butterknife.OnClick;

public class EqpUnlockActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.teEqpPosition)
    TextView teEqpPosition;
    @BindView(R.id.teEqpType)
    TextView teEqpType;
    @BindView(R.id.ivLockArrow)
    ImageView ivLockArrow;
    @BindView(R.id.flLock)
    FrameLayout flLock;
    @BindView(R.id.tvLockText)
    TextView tvLockText;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private FreeUI_GeneralPop codePop;
    private List<String> lockList;
    private String lockType = "手动解锁";
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqpunlock;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("解锁");
        lockList = new ArrayList<>();
        lockList.add("手动解锁");
        lockList.add("开启远程控制");
        lockList.add("关闭远程控制");

    }

    @Override
    protected void initData() {
        initEdit(etEqpId, this::getEqpInfo);
    }

    private void getEqpInfo(String eqpID) {
        FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    UpdateIPEntity body = response.body().getResponse().getBody();
                    teEqpPosition.setText(body.getPOSITION());
                    teEqpType.setText(body.getEQPTYPE());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEquipment");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQPID",eqpID);
        NetUtils.requestPostNet("EAP.GETEQPINFOBYEQPID","GETEQPINFOBYEQPID", EqpUnlockActivity.this,map,callBack);

    }

    @OnClick({R.id.tvBaseSubmit,R.id.flLock})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (getText(etEqpId).equals("")){
                    showToast("请先扫描设备ID",false);
                    return;
                }
                FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定要解锁吗?", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        FragmentManager supportFragmentManager = getSupportFragmentManager();
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    if ("手动解锁".equals(lockType)) {
                                        showToast("解锁成功", true);
                                    }else {
                                        showToast("操作成功", true);
                                    }
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(),"LOCK");
                            }
                        };
                        String messageName = "";
                        String method = "";
                        String eqpCmdName = "";
                        switch (lockType){
                            case "手动解锁":
                                messageName = "IF.ReleaseEqp";
                                method = "RestAPI/ReleaseEqp";
                                eqpCmdName = "ReleaseEqp";
                                break;
                            case "开启远程控制":
                                messageName = "SendEqpCommand";
                                method = "RestAPI/RemoteEqp";
                                eqpCmdName = "RemoteEqp";
                                break;
                            case "关闭远程控制":
                                messageName = "SendEqpCommand";
                                method = "RestAPI/LocalEqp";
                                eqpCmdName = "LocalEqp";
                                break;
                        }

                        JsonObject map = new JsonObject();
                        map.addProperty("eqpID",getText(etEqpId));
                        map.addProperty("EqpCmdName",eqpCmdName);
                        map.addProperty("EqpCmdParam","");
                        map.addProperty("EqpType", (String) tvLockText.getText());
                        NetUtils.requestPostNet(messageName,method,EqpUnlockActivity.this,map,callBack);
                    }
                }, () -> {

                }).show(getSupportFragmentManager(),"");
                break;
            case R.id.flLock:

                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flLock.getWidth(), lockList, ivLockArrow, (codeS, i) -> {
                        tvLockText.setText(codeS);
                        lockType = codeS;
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivLockArrow);
                codePop.showAsDropDown(flLock, 0, 0);
                break;

        }

    }
}
